using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ShareScreenshot : MonoBehaviour
{
	private Color grayColor = new Color(45f / 128f, 45f / 128f, 45f / 128f);

	public Camera cam;

	public Text levelNumberText;

	public Text[] levelTimeText;

	public Image[] pointsImage;

	public Image newRecordImage;

	private static ShareScreenshot instance;

	private static string screenshotFileName = "share_screenshot.png";

	private bool hasScreenshot;

	private int width = 990;

	private int height = 612;

	public static string screenshotImagePath
	{
		get
		{
			if (instance.hasScreenshot)
			{
				return Application.persistentDataPath + "/" + screenshotFileName;
			}
			return string.Empty;
		}
	}

	private void Awake()
	{
		if ((bool)cam)
		{
			cam.enabled = false;
		}
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public static void TakeScreenshot(int levelNumber, float levelTime, bool isRecord)
	{
		int hundreds = 0;
		int minutes;
		int seconds;
		Utils.GetSplitedTime(levelTime, out minutes, out seconds, out hundreds);
		int numberOfPointsForTime = Economy.GetNumberOfPointsForTime(levelNumber, levelTime);
		instance.TakeScreenShotI(levelNumber, minutes, seconds, hundreds, numberOfPointsForTime, isRecord);
	}

	public static void TakeScreenshotInstant(int levelNumber, float levelTime, bool isRecord)
	{
		int hundreds = 0;
		int minutes;
		int seconds;
		Utils.GetSplitedTime(levelTime, out minutes, out seconds, out hundreds);
		int numberOfPointsForTime = Economy.GetNumberOfPointsForTime(levelNumber, levelTime);
		instance.TakeScreenShotIInstant(levelNumber.ToString(), minutes, seconds, hundreds, numberOfPointsForTime, isRecord);
	}

	private void TakeScreenShotIInstant(string levelNumber, int m, int s, int h, int points, bool isRecord)
	{
		if (cam == null)
		{
			return;
		}
		cam.enabled = true;
		levelNumberText.text = levelNumber;
		levelTimeText[0].text = m.ToString("D2");
		levelTimeText[1].text = s.ToString("D2");
		levelTimeText[2].text = h.ToString("D2");
		for (int i = 1; i <= pointsImage.Length; i++)
		{
			if (i <= points)
			{
				pointsImage[i - 1].color = Color.white;
			}
			else
			{
				pointsImage[i - 1].color = grayColor;
			}
		}
		newRecordImage.gameObject.SetActive(isRecord);
		RenderTexture active = RenderTexture.active;
		RenderTexture temporary = RenderTexture.GetTemporary(width, height);
		cam.targetTexture = temporary;
		RenderTexture.active = temporary;
		cam.Render();
		string path = Application.persistentDataPath + "/" + screenshotFileName;
		Texture2D texture2D = new Texture2D(width, height);
		texture2D.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
		texture2D.Apply();
		cam.targetTexture = null;
		RenderTexture.ReleaseTemporary(temporary);
		RenderTexture.active = active;
		hasScreenshot = true;
		cam.enabled = false;
		File.WriteAllBytes(path, texture2D.EncodeToPNG());
		texture2D = null;
	}

	private void TakeScreenShotI(int levelNumber, int m, int s, int h, int points, bool isRecord)
	{
		StartCoroutine(TakeScreenShotICoroutine(levelNumber, m, s, h, points, isRecord));
	}

	private IEnumerator TakeScreenShotICoroutine(int levelNumber, int m, int s, int h, int points, bool isRecord)
	{
		yield return true;
		if (cam == null)
		{
			yield break;
		}
		cam.enabled = true;
		levelNumberText.text = levelNumber.ToString();
		levelTimeText[0].text = m.ToString("D2");
		levelTimeText[1].text = s.ToString("D2");
		levelTimeText[2].text = h.ToString("D2");
		for (int i = 1; i <= pointsImage.Length; i++)
		{
			if (i <= points)
			{
				pointsImage[i - 1].color = Color.white;
			}
			else
			{
				pointsImage[i - 1].color = grayColor;
			}
		}
		newRecordImage.gameObject.SetActive(isRecord);
		RenderTexture activeRenderTexture = RenderTexture.active;
		RenderTexture tempRT = RenderTexture.GetTemporary(width, height);
		cam.targetTexture = tempRT;
		RenderTexture.active = tempRT;
		cam.Render();
		string fullPath = Application.persistentDataPath + "/" + screenshotFileName;
		Texture2D tex = new Texture2D(width, height);
		tex.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
		tex.Apply();
		cam.targetTexture = null;
		RenderTexture.ReleaseTemporary(tempRT);
		RenderTexture.active = activeRenderTexture;
		hasScreenshot = true;
		cam.enabled = false;
		yield return true;
		File.WriteAllBytes(fullPath, tex.EncodeToPNG());
		tex = null;
	}
}
